(define (abs x) (if (< x 0) (-  x) x))
(define (square x) (* x x))

; (define (sqrt x)
;   (the y (and (>= y 0)
;               (= (square y) x))))

; This only begs the question.

; The contrast between function and procedure is a reflection of the general distinction between describing properties of things and describing how to do things, or, as it is sometimes referred to, the distinction between declarative knowledge and imperative knowledge. In mathematics we are usually concerned with declarative (what is) descriptions, whereas in computer science we are usually concerned with imperative (how to) descriptions.20

; How does one compute square roots? The most common way is to use Newton's method of successive approximations, which says that whenever we have a guess y for the value of the square root of a number x, we can perform a simple manipulation to get a better guess (one closer to the actual square root) by averaging y with x/y.21 For example, we can compute the square root of 2 as follows. Suppose our initial guess is 1:

; Guess 	Quotient 	Average
  
; 1 	(2/1) = 2 	((2 + 1)/2) = 1.5
  
; 1.5 	(2/1.5) = 1.3333 	((1.3333 + 1.5)/2) = 1.4167
  
; 1.4167 	(2/1.4167) = 1.4118 	((1.4167 + 1.4118)/2) = 1.4142
  
; 1.4142 	...	...

; Continuing this process, we obtain better and better approximations to the square root.

; Now let's formalize the process in terms of procedures. We start with a value for the radicand (the number whose square root we are trying to compute) and a value for the guess. If the guess is good enough for our purposes, we are done; if not, we must repeat the process with an improved guess. We write this basic strategy as a procedure:

(define (sqrt-iter guess x)
  (if (good-enough? guess x)
      guess
      (sqrt-iter (improve guess x)
                 x)))

; A guess is improved by averaging it with the quotient of the radicand and the old guess:

(define (improve guess x)
  (average guess (/ x guess)))

; where

(define (average x y)
  (/ (+ x y) 2))

; We also have to say what we mean by ``good enough.'' The following will do for illustration, but it is not really a very good test. (See exercise 1.7.) The idea is to improve the answer until it is close enough so that its square differs from the radicand by less than a predetermined tolerance (here 0.001):22

(define (good-enough? guess x)
  (< (abs (- (square guess) x)) 0.001))

; Finally, we need a way to get started. For instance, we can always guess that the square root of any number is 1:23

(define (sqrt x)
  (sqrt-iter 1.0 x))